/**    
 * @Description: 博客园地址：https://www.cnblogs.com/zumengjie/      
 * @author: 顶风少年   
 * @date: 2021年4月4日 下午6:13:03
 */

package datang.dtdb.datasource.pooled;

import java.util.Properties;

import javax.sql.DataSource;

import datang.dtdb.datasource.DataSourceFactory;

/**
 * 获取一个连接池
 */
public class PooledDataSourceFactory implements DataSourceFactory {

	/**
	 * 数据源
	 */
	private PooledDataSource dataSource;

	public PooledDataSourceFactory() {
		dataSource = new PooledDataSource();
	}

	/**
	 * 设置必备的属性
	 */
	@Override
	public void setProperties(Properties props) {
		if(props!=null) {
			dataSource.dataSource.setDriverClassName(props.getProperty("driver"));
			dataSource.dataSource.setUrl(props.getProperty("url"));
			dataSource.dataSource.setUsername(props.getProperty("username"));
			dataSource.dataSource.setPassword(props.getProperty("password"));
			String coresize = props.getProperty("coreSize");
			if (coresize != null) {
				dataSource.setCoreSize(Integer.parseInt(coresize));
			}
			dataSource.initPool();
		}	
	}

	/**
	 * 创建DruidDataSource连接池
	 */
	@Override
	public DataSource getDataSource() {
		return dataSource;
	}
}
